﻿
using Controller.i18n.entity;
using HBase;
using hCommand;
using hconnect;
using Persistence;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Controller {

    public class CarrierAllocateJobTask1 : CarrierAllocateJobTask {
        public override bool excute() {
            AnalysisCarrier(ConstStn.stn1, ConstStn.stn1.GetCarrierAddr());
            
            return true;
        }
    }
    public class CarrierAllocateJobTask2 : CarrierAllocateJobTask {
        public override bool excute() { 
            AnalysisCarrier(ConstStn.stn3, ConstStn.stn3.GetCarrierAddr()); 
            return true;
        }
    }
    public class CarrierAllocateJobTask3 : CarrierAllocateJobTask {
        public override bool excute() { 
            AnalysisCarrier(ConstStn.stn5, ConstStn.stn5.GetCarrierAddr()); 
            return true;
        }
    }
    public class CarrierAllocateJobTask4 : CarrierAllocateJobTask {
        public override bool excute() { 
            AnalysisCarrier(ConstStn.stn7, ConstStn.stn7.GetCarrierAddr()); 
            return true;
        }
    }
    public class CarrierAllocateJobTask5 : CarrierAllocateJobTask {
        public override bool excute() { 
            tuopanOutStn(ConstStn.Crane1Out1Stn); 
            return true;
        }
    }
    public class CarrierAllocateJobTask6 : CarrierAllocateJobTask {
        public override bool excute() { 
            tuopanOutStn(ConstStn.Crane1Out2Stn);
            return true;
        }
    }
    public abstract class CarrierAllocateJobTask : JobAbleTask {
        protected CarrierConnect1 connect;
        protected ICSDServer csdServer = WMSDalFactray.getDal<ICSDServer>();
        protected ICoreStockServer stockServer = WMSDalFactray.getDal<ICoreStockServer>();

        public override SocketZtTaskConnect getTaskConnect(BaseTask taskModel) {
            return connect;
        }
        protected override bool onlyOneTimeAsync() {
            base.onlyOneTimeAsync();
            time = 500;
            return true;
        }
        private  List<T> query<T>(Func<T, bool> func) where T : BaseTask {
            List<T> baselist = MyCache.getList<T>();
            List<T> res = baselist.Where(a => func(a)).ToList();
            return res;
        }

        List<TaskCrane> getBefore(TaskCarrier t2) {
            String info = "me"+t2.completeId+"t"+t2.taskNo+"all";
         
            List<TaskCrane> beforeList = new List<TaskCrane>();
            List<TaskCrane> allList = MyCache.getList<TaskCrane>();
            foreach (TaskCrane a in allList) {
                info += a.completeId + "t" + t2.taskNo + "to"+ a.des + ";";
                if (a.des == 21) {
                    a.des = a.des;
                }
             
                if (a.completeId == t2.completeId) {
                    if (a.des == t2.src|| a.des+3 == t2.src) {
                        beforeList.Add(a);
                    }

                }

            }
            if (beforeList.Count == 0) {
                consoleOnly(0, info);
            }
          
            return beforeList;
        }

        public List<TaskCarrier> getTasksByStn(int stn) {

            List<TaskCarrier> list = new List<TaskCarrier>();
            List<TaskCarrier> list2 = query<TaskCarrier>((a) => a.src == stn && a.status == 11);
            foreach (TaskCarrier t2 in list2) {
                List<TaskCrane> beforeList = getBefore(t2);
                if (beforeList.Count == 0) {
                    consoleOnly(0, "no before" + t2.taskNo);
                    list.Add(t2);
                } else if (beforeList.Count == 1 && beforeList[0].status == 80) {
                    consoleOnly(0, beforeList[0].src + "before" + t2.taskNo);
                    list.Add(t2);
                } else if (beforeList.Count > 1) {
                    String info = "";
                    foreach (TaskCrane b in beforeList) {
                        info += b.src + "-" + b.taskNo + ",";
                    }
                    consoleOnly(0, info + "before" + t2.taskNo);
                }
            }

            return list;

        }
        public void tuopanOutStn(int stn) { 
            List<TaskCarrier> carriers =  getTasksByStn(stn);
            if (carriers.Count > 0) {
                var info2 = stn + "待执行任务";
                foreach (var c in carriers) {
                    info2 += c.taskNo + ",";
                }
               consoleOnly(0, info2);
            }
         
            if (carriers.Count == 0) {
                CarrierSignalStatus ss = StatusCache.getSignalStatus<CarrierSignalStatus>(stn);
                if (ss == null) {
                    consoleOnly(1, stn + "状态异常" );
                    return;
                
                }
                if (ss.isArrived()) {
                    List<CoreStock> csList = stockServer.getCSBypkStn(stn);
                    if (csList.Count > 1) {
                        String info = "";
                        foreach (var c in csList) {
                            info += c.boxCode + ",";
                        }
                        consoleOnly(2, stn + "存在多个托盘" + info);
                        return;
                    }
                    if (csList.Count == 0) {
                        consoleOnly(3, stn + "无托盘");
                        return;
                    }
                    var cs = csList[0];
                    var res = WMSTransactionFacade.doStockinLocEngine(cs.boxCode, stn,  cs.eigen);
                    consoleOnly(4, stn + "生成任务" + res.msg);
                } else {
                    consoleOnly(7, stn + "没有到位");
                }
              
            } else {
                consoleOnly(5, stn + "有任务" + carriers[0].taskNo);
            }

        }

      public  void AnalysisCarrier(int stn, short addr) {
            CarrierSignalStatus carrierSignal = StatusCache.getSignalStatus<CarrierSignalStatus>(stn);

            CarrierConnect1 carrierConnect1 = TaskPool.get<CarrierConnect1>();
            if (carrierSignal == null) {
                consoleOnly(InfoC.consoleOnly_doScan_ss_null, stn + "站台信号异常");
                return;
            }
        
            ScanNetSynchro scaner = TaskPool.get<ScanNetSynchro>(stn);
            KanBanConnect banConnect = TaskPool.get<KanBanConnect>(stn + 100);
            if (carrierSignal.IsScanArrive())//到位
            {
                var taskParam = MyCache.query<CoreTaskParam>(a => a.cDes == stn && a.status > 11 && a.status < 80).FirstOrDefault();

                if (taskParam != null) {
                    carrierConnect1.CarrierWriteStatus(2, addr);
                    banConnect.LedFormatInfoBack("存在出库任务");
                    return;
                }
                scaner.beginScan();

                if (scaner.isOutOffTime())
                {
                    carrierConnect1.CarrierWriteStatus(2, addr);
                    banConnect.LedFormatInfoBack("未扫到码" + scaner.t);
                    return;
                }
                if (  scaner.err > 8)
               {
                    carrierConnect1.CarrierWriteStatus(2, addr);
                    banConnect.LedFormatInfoBack("扫码异常" + scaner.err);
                    return;
                }
                string code = scaner.getCode();
                if (!string.IsNullOrEmpty(code)) {
                    scanCode(code, stn, carrierSignal, addr, carrierConnect1, banConnect);
                }
            } else {
                //LoggerCommon.consol(100, stn + "无扫码到位");
                scaner.stopScan();
            }


        }

        void scanCode(String code,int stn, CarrierSignalStatus carrierSignal, short addr, CarrierConnect1 carrierConnect1, KanBanConnect banConnect) {
            CoreStock coreStock = stockServer.getCoreStockByCode(code);
            if (coreStock != null) {
                code = code.Substring(0, 6);
                int eigen = 0;
                if (stn < 30) {
                    if (carrierSignal.inWeightValue > 1000 || carrierSignal.inHeightStatus == 1) {
                        eigen = 2;
                    } else {
                        eigen = 3;
                    }
                } else {
                    eigen = 1;
                }
                TransRes result = WMSTransactionFacade.doStockinLocEngine(code, stn, eigen);
                if (result != null && result.s) {
                    carrierConnect1.CarrierWriteStatus(1, addr);
                    //scaner.ClearScan();
                    banConnect.LedFormatInfoBack(code + "任务已生成");
                } else if (result != null && !result.s) {
                    carrierConnect1.CarrierWriteStatus(2, addr);
                    banConnect.LedFormatInfoBack(result.msg);
                }
                LoggerCommon.consol(100, result.msg);
            } else {
                carrierConnect1.CarrierWriteStatus(2, addr);
                banConnect.LedFormatInfoBack("查不到托盘号"+code);
            }

        }

       
    }
}
